<?php

namespace app\controller;

use support\Request;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use app\service\BaseService;
abstract class ApiController
{
    protected BaseService $service;

    abstract public function getService(): BaseService;

    public function __construct()
    {
        $this->service = $this->getService();
    }

    /**
     * 新增通用逻辑
     * @param Request $request
     * @return Response
     */
    public function create(Request $request)
    {
        $res = $this->service->create($request->all());
        return json(success_response('create success', $res));
    }

    /**
     * 更新通用逻辑
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function update(Request $request)
    {
        $res = $this->service->update($request->all());
        return json(success_response('update success', $res));
    }

    /**
     * 删除通用逻辑
     * @param Request $request
     * @return Response
     */
    public function delete(Request $request)
    {
        $res = $this->service->delete($request->all());
        return json(success_response('delete success', $res));
    }

    /**
     * 查询通用逻辑
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function select(Request $request)
    {
        $res = $this->service->select($request->all());
        return json(success_response('query success', $res));
    }

    /**
     * 查询单条通用逻辑
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function find(Request $request)
    {
        $res = $this->service->find($request->all()['param']['filter']['id']);
        return json(success_response('query success', $res));
    }
}
